package com.hks360.car_treasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Constant;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.model.Add.User;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenter;
import com.hks360.car_treasure.mvp.user.presenter.UserPresenterImpl;
import com.hks360.car_treasure.mvp.user.view.UserView;
import com.hks360.car_treasure.socket.CommunicationManager;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;

public class WelcomeActivity extends BaseActivity implements UserView {
    private boolean isFirstOpen;
    private UserPresenter mUserPresenter;

    private Runnable openRunnable = new Runnable() {
        @Override
        public void run() {
            openActivity();
        }
    };

    private void openActivity() {
        if(isFirstOpen) {
            mUserPresenter.login(1);

        } else {
            CommonUtil.openActivity(this, LoginActivity.class);
            finish();
        }
    }
    private void setServer() {
        AppConfig.mComManager.setADDRESS(Method.getServer().split(":")[0].trim());
        AppConfig.mComManager.setPOT(Integer.valueOf(Method.getServer().split(":")[1].trim()));
        Log.e("RWQ111", Method.getServer().split(":")[0]);
        Log.e("RWQ111",Method.getServer().split(":")[1]);
    }


    public void setupView() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


    public void initData() {
        isFirstOpen = PrefUtil.getBoolean(this, PrefKey.LOGIN_IS_AUTO,false);
        mUserPresenter = new UserPresenterImpl(this,this);
        new Handler().postDelayed(openRunnable, Constant.DELAY_DEFAULT);
    }


    public void addListener() {

    }


    public void initView() {
        setContentView(R.layout.layout_welcome);
        initData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }


    public void viewClick(View v) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public String getMobile() {
        return PrefUtil.getString(this,PrefKey.LOGIN_PHONE,"");
    }

    @Override
    public String getCode() {
        return null;
    }

    @Override
    public String getPassword() {
        return PrefUtil.getString(this,PrefKey.LOGIN_PASSWORD,"");
    }

    @Override
    public void loginSuccess(User user) {
        Method.setI(PrefUtil.getInt(this,PrefKey.COUNT,0));
        AppConfig.user = user;
        PrefUtil.putString(this, PrefKey.TRUENAME, user.getTruename().trim());
        PrefUtil.putString(this, PrefKey.EMAIL, user.getEmail().trim());
        CommonUtil.openActivity(this, MainActivity.class);
        PrefUtil.putString(this, PrefKey.TBOX_ID, Method.getTboxId());
        if (AppConfig.user.getTboxinfo().size() != 0) {
            while (AppConfig.mComManager == null){
                try{
                    AppConfig.mComManager = new CommunicationManager(this);
                    setServer();
                    AppConfig.mComManager.loading(Method.getTboxId(), PrefUtil.getString(this, PrefKey.LOGIN_PHONE, ""), PrefUtil.getString(this, PrefKey.LOGIN_PASSWORD, ""));
                    AppConfig.mComManager.startHeartBeat();
                }catch (Exception e){

                }

            }
        }else {
            AppConfig.mComManager = new CommunicationManager(this);
        }
        finish();
    }

    @Override
    public void showToastMsg(String msg) {

    }

    @Override
    public void sendCheckCodeSuccess() {

    }

    @Override
    public void findPasswordSuccess() {

    }

    @Override
    public void clearView() {
        CommonUtil.openActivity(this,LoginActivity.class);
    }

    @Override
    public void sendCheckCodeFailed() {

    }
}
